package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hscrb.bean.MjSterilization;
import com.jy.adscm.operation.hscrb.bean.MjSterilization;
import com.jy.adscm.operation.hscrb.mapper.mysql.MjSterilizationMapper;
import com.jy.adscm.operation.hscrb.service.MjSterilizationService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-17
 */
@Service
public class MjSterilizationServiceImpl extends ServiceImpl<MjSterilizationMapper, MjSterilization> implements MjSterilizationService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateMjSterilization(MjSterilization mjSterilization, Token token) throws AdscmSystemException {
        //待办修改
        if (mjSterilization.getTaskstate()==0 && mjSterilization.getTaskfinishstate()==0 &&
                "0".equals(mjSterilization.getIstaskstart()) &&  "0".equals(mjSterilization.getIstaskbegin()) &&
                "0".equals(mjSterilization.getIstaskend())  && "0".equals(mjSterilization.getIsreviewtask())
        ){


            return  super.updateById(mjSterilization);
        }
        //接收任务
        if (mjSterilization.getTaskstate()==0 && mjSterilization.getTaskfinishstate()==0 &&
                "1".equals(mjSterilization.getIstaskstart()) &&  "0".equals(mjSterilization.getIstaskbegin()) &&
                "0".equals(mjSterilization.getIstaskend())  && "0".equals(mjSterilization.getIsreviewtask())
        ){
            mjSterilization.setIstaskstart("1");
            mjSterilization.setTaskstartaddtime(new Date());
            mjSterilization.setTaskstate(1);

            return  super.updateById(mjSterilization);
        }
        //开始任务
        if (mjSterilization.getTaskstate()==1 && mjSterilization.getTaskfinishstate()==0 &&
                "1".equals(mjSterilization.getIstaskstart()) &&  "1".equals(mjSterilization.getIstaskbegin()) &&
                "0".equals(mjSterilization.getIstaskend())  && "0".equals(mjSterilization.getIsreviewtask())
        ){
            mjSterilization.setIstaskbegin("1");
            mjSterilization.setTaskbeginaddtime(new Date());
            return super.updateById(mjSterilization);
        }
        //结束任务
        if (mjSterilization.getTaskstate()==1 && mjSterilization.getTaskfinishstate()==0 &&
                "1".equals(mjSterilization.getIstaskstart()) &&  "1".equals(mjSterilization.getIstaskbegin()) &&
                "1".equals(mjSterilization.getIstaskend())  && "0".equals(mjSterilization.getIsreviewtask())
        ){
            mjSterilization.setIstaskend("1");
            mjSterilization.setTaskfinishstate(1);
            mjSterilization.setTaskendaddtime(new Date());
            return super.updateById(mjSterilization);
        }
        //复核任务
        if (mjSterilization.getTaskstate()==1 && mjSterilization.getTaskfinishstate()==1 &&
                "1".equals(mjSterilization.getIstaskstart()) &&  "1".equals(mjSterilization.getIstaskbegin()) &&
                "1".equals(mjSterilization.getIstaskend())  && "1".equals(mjSterilization.getIsreviewtask())
        ){
            mjSterilization.setIsreviewtask("1");
            mjSterilization.setReviewaddtime(new Date());
            mjSterilization.setReviewperson(token.getUser().getId().toString());
            return super.updateById(mjSterilization);
        }

        return null;
    }

    @Override
    public List<MjSterilization> selectMjSterilization(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<MjSterilization> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<MjSterilization> mjSterilizations= super.selectList(wrapper);



        for (MjSterilization mjSterilization : mjSterilizations) {
            if ( mjSterilization.getTaskreleaseperson()!=null && !"".equals( mjSterilization.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(mjSterilization.getTaskreleaseperson());


                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    mjSterilization.setTaskreleasepersonName(jyUser.getName());
                }else {
                    mjSterilization.setTaskreleasepersonName(mjSterilization.getTaskreleaseperson());
                }

            }
            if ( mjSterilization.getTaskperformperson()!=null && !"".equals( mjSterilization.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(mjSterilization.getTaskperformperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    mjSterilization.setTaskperformpersonName(jyUser.getName());
                }else {
                    mjSterilization.setTaskperformpersonName(mjSterilization.getTaskperformperson());
                }

            }

            if ( mjSterilization.getReviewperson()!=null && !"".equals( mjSterilization.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(mjSterilization.getReviewperson());

                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    mjSterilization.setReviewpersonName(jyUser.getName());
                }else {
                    mjSterilization.setReviewpersonName(mjSterilization.getReviewperson());
                }

            }

        }

        return mjSterilizations;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("108");
        return jyUsers;
    }
}
